﻿#include <iostream>
#include <fstream> //定义读写已命名文件的类型
#include <vector>
#include <sstream> //多定义的类型则用于读写存储在内存中的string对象
#include <time.h> 
#include "omp.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>//strrchr用到 
#include <string>
#include<string>
#include<algorithm>


using namespace std;


struct Client {
	string from_tx;
	string to_tx;
	int timestamp_tx;
	float value_tx;
	int edgetype_tx;
};



//InsertionSort.cpp
vector<Client> InsertSortPart(vector<Client>& vec);				//插入排序		依据时间戳
vector<Client> InsertSortPartParallel(vector<Client>& vec);		//并行插入排序	依据时间戳
vector<Client> InsertSortPartValue(vector<Client>& vec);		//插入排序		依据金额
vector<Client> InsertSortPartParallelValue(vector<Client>& vec);//并行插入排序	依据金额

//SelfSort.cpp
vector<Client> SelfSortPart(vector<Client>& vec);				//vector自带排序		Timestamp 
vector<Client> SelfSortPartParallel(vector<Client>& vec);		//并行vector自带排序	Timestamp
vector<Client> SelfSortPartValue(vector<Client>& vec);			//vector自带排序		Value
vector<Client> SelfSortPartParallelValue(vector<Client>& vec);	//并行vector自带排序	Value
//快速排序


int main()
{
	//---------------------------------------读取文件开始--------------------------------------------//
	ifstream inFile("D:\\备份文件夹\\Small_Account_TX - 副本.csv", ios::in);
	//ifstream inFile("D:\\备份文件夹\\Account_TX - 副本.csv", ios::in);
	string lineStr;
	vector<Client> vectorClient;
	int num = 0;

	while (getline(inFile, lineStr)) {//getline来自sstream		//读入整行数据到Linestr里
		stringstream ss(lineStr);//来自sstream
		string str;
		if (num > 0)
		{
			//按照逗号分隔
			Client client;
			int n1 = 1;
			while (getline(ss, str, ','))
			{
				switch (n1)
				{
				case 1:
					client.from_tx = str;
					break;
				case 2:
					client.to_tx = str;
					break;
				case 3:
					client.timestamp_tx = atoi(str.c_str());
					break;
				case 4:
					client.value_tx = atof(str.c_str());
					break;
				}
				n1++;
			}
			vectorClient.push_back(client);//每一行vector数据都放到strArray中去
		}
		num++;
	}
	//---------------------------------------读取文件结束--------------------------------------------//


	//---------------------------------------选择排序依据--------------------------------------------//
	double cost = 0;
	//输入KEY
	cout << "请输入排序的KEY" << endl;
	cout << "可输入timestamp，value两种排序选择，输入exit退出" << endl;
	string key;
	cin >> key;
	int flag = 1;//默认按照id排序
	if (key == "value")
	{
		flag = 2;
	}
	else if (key == "exit")
	{
		cout << "不排序，即将退出" << endl;
		getchar();//（防止程序一闪而过）
		return 0;
	}
	//---------------------------------------选择依据结束--------------------------------------------//
	
	/***********备份数据 防止排序互相干扰***************/
	vector<Client> vectorClient1 = vectorClient;
	vector<Client> vectorClient3 = vectorClient;
	vector<Client> vectorClient4 = vectorClient;
	vector<Client> vectorClient2;

	//---------------------------------------自带排序开始--------------------------------------------//
	cout << "————使用自带方法排序后————" << endl;
	clock_t begin = clock();
	switch (flag)
	{
	case 1:
		vectorClient2 = SelfSortPart(vectorClient);
		cout << "根据 timestamp 排序" << endl;
		break;

	case 2:
		vectorClient2 = SelfSortPartValue(vectorClient);
		cout << "根据 Value 排序" << endl;
		break;
	}
	clock_t end = clock();
	cost = (double)(end - begin);
	cout << "串行排序时间" << cost << endl;

	//开始并行执行
	begin = clock();
	switch (flag)
	{
	case 1:
		SelfSortPartParallel(vectorClient1);
		cout << "根据 timestamp 排序" << endl;
		break;

	case 2:
		SelfSortPartParallelValue(vectorClient1);
		cout << "根据 Value 排序" << endl;
		break;
	}
	
	end = clock();
	cost = (double)(end - begin);
	cout << "并行排序时间" << cost << endl;
	//---------------------------------------自带排序结束--------------------------------------------//
	
	
	
	
	
	
	//---------------------------------------插入排序开始--------------------------------------------//
	cout << "————使用插入排序方法排序———" << endl;
	begin = clock();
	switch (flag)
	{
	case 1:
		vectorClient2 = InsertSortPart(vectorClient3);
		cout << "根据 timestamp 排序" << endl;
		break;

	case 2:
		vectorClient2 = InsertSortPartValue(vectorClient3);
		cout << "根据 Value 排序" << endl;
		break;
	}
	end = clock();
	cost = (double)(end - begin);
	cout << "串行排序时间" << cost << endl;

	//开始并行执行
	begin = clock();

	switch (flag)
	{
	case 1:
		InsertSortPartParallel(vectorClient4);
		cout << "根据 timestamp 排序" << endl;
		break;

	case 2:
		InsertSortPartParallelValue(vectorClient4);
		cout << "根据 Value 排序" << endl;
		break;
	}

	end = clock();
	cost = (double)(end - begin);
	cout << "并行排序时间" << cost << endl;
	//---------------------------------------插入排序结束--------------------------------------------//
	










	/*******************打印输出*************/
	
	cin >> key;
	//排序后  输出结果
	for (vector<Client>::iterator it = vectorClient2.begin(); it != vectorClient2.end(); it++)
	{
		cout << "from_tx: " << it->from_tx << " to_tx: " << it->to_tx << " timestamp_tx: " << it->timestamp_tx << " value_tx: " << it->value_tx << endl;
	}
	getchar();//（防止程序一闪而过）
	return 0;
}


